var baseUrl = require('../../utils/urllink.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_id: app.globalData.shop_id,
    goods_list: [],
    goods_id: 0,
    page: 1,
    page_size: 10,
    is_on_sale: 0,
    agent_type: 0,
    is_show:false,
    hostimgs: app.globalData.hostimgs,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  //初始化
  onLoad: function (options) {
    var that = this
    this.setData({
      agent_type: app.globalData.agent_type,
      shop_id: app.globalData.shop_id,
    })
    //获取店铺id
    getshop(that, app.globalData.user_id)



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    that.setData({
      page: 1,
    })
    //查询商品集合
    goods_list(that, that.data.shop_id, 1, that.data.page_size)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    //查询商品集合
    goods_list(that, that.data.shop_id, that.data.page, that.data.page_size)
  },

  //跳转到我的店铺页面
  myshop:function(e){
    wx.navigateTo({
      url: '../dianpu/dianpu_goods_list',
    })
  },
  //跳转到店铺商品详情
  selectgoods:function(e){
    var that = this
    var goods_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../goods/detail?goods_id='+goods_id,
    })
  },
  //删除商品
  delgoods: function (e) {
    var that = this
    var goods_id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '您确定要取消推广吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          delgoods(that, that.data.shop_id, goods_id)//删除商品
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  // 店铺商品上架 下架
  goods_isshow: function (e) {
    var that = this;
    var goods_id = e.currentTarget.dataset.id;

    //调用上架 下架接口
    wx.request({
      url: baseUrl + 'weapp/ShopGoods/on_sale.html',
      data: {
        goods_id: goods_id,
        shop_id:that.data.shop_id,
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: { 'content-type': 'application/json' }, // 设置请求的 header
      success: function (res) {
        console.log(res.data)
        //成功提示
        if (res.data.code == 0) {
          //下架
          wx.showToast({
            title: '成功下架',
          })
        } else {
          // 1 上架
          wx.showToast({
            title: '成功上架',
          })
        }

      },
      fail: function () { },
      complete: function () { }
    })

    //修改 goods_list 集合中 的字段值
    var goods_list = that.data.goods_list;
    for (var i = 0; i < goods_list.length; i++) {
      if (goods_list[i].goods_id == goods_id) {
        if (goods_list[i].is_show == 1) {
          goods_list[i].is_show = 0;
        } else {
          goods_list[i].is_show = 1;
        }
      }
    }
    that.setData({
      goods_list: goods_list,
    })
  },

})


//查询 店铺商品集合
function goods_list(that, shop_id, page, page_size) {
  wx.showLoading({
    title: '加载中...',
  })
  //请求服务器
  wx.request({
    url: baseUrl + 'weapp/ShopGoods/goods_list.html',

    data: {
      shop_id: shop_id,
      page: page,
      page_size, page_size,
      type: 0,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if(page == 1){
        if (res.data.code == -1) {
          wx.showModal({
            title: '提示',
            content: '您还没有提交店铺资料，请去完善资料吧！',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.switchTab({
                  url: '../mine/mine',
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
          that.setData({
            is_show: true,
          })
        } else if (res.data.code == -2) {
          that.setData({
            is_show: true,
          })

        }else{
          //第一次
          that.setData({
            goods_list: res.data,
            page: page + 1,
            is_show: false,
          })
        }
      }else{
        if (res.data.code == -1) {
          wx.showModal({
            title: '提示',
            content: '您还没有提交店铺资料，请去完善资料吧！',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.switchTab({
                  url: '../mine/mine',
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.code == -2) {
          that.setData({
            is_show: false,
          })

        } else {
          //分页
          var goods_list = that.data.goods_list;
          for (var i = 0; i < res.data.length; i++) {
            goods_list.push(res.data[i])
          }

          that.setData({
            goods_list: goods_list,
            page: page + 1,
            is_show: false,
          })
        }
      }
      
      
      
      wx.hideLoading();


    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//删除 店铺的商品
function delgoods(that, shop_id, goods_id) {

  //请求服务器
  wx.request({
    url: baseUrl + 'weapp/ShopGoods/delete_shop_goods.html',
    data: {
      shop_id: shop_id,
      goods_id: goods_id,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data.code == 0) {
        wx.showToast({
          title: '删除成功',
        })
        that.setData({
          goods_list: [],
        })
        //查询店铺商品集合
        goods_list(that, shop_id, 1, that.data.page_size)
      } else {
        wx.showToast({
          title: '删除失败',
        })
      }



    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}




// 获取店铺信息 (有店铺信息 返回信息，没有店铺 返回)
function getshop(that, user_id) {
  //请求服务器
  wx.request({
    url: baseUrl + 'weapp/Shop/shop_data.html',
    data: {
      user_id: user_id,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data.code == -1) {
        that.setData({
          shop_id: 0,
        })
        wx.showModal({
          title: '提示',
          content: '您还没有提交店铺资料，请去完善资料吧！',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.switchTab({
                url: '../mine/mine',
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        //返回数据值
        that.setData({
          shop_id: res.data.shop_id,
        })
        //获取店铺下商品集合
        goods_list(that, res.data.shop_id, 1, that.data.page_size) 
      }
    },
    fail: function () { },
    complete: function () { }
  })
}